﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System.IO.IsolatedStorage;

namespace wallstreet
{
    public partial class MainPage : PhoneApplicationPage
    {
        ApplicationBarMenuItem txtLastUpdated = null;

        public MainPage()
        {
            InitializeComponent();

            //http://feeds.finance.yahoo.com/rss/2.0/category-international?region=US&lang=en-US

            Color color = new Color();
            color.A = 0;
            color.B = 0;
            color.G = 0;
            color.R = 0;

            ApplicationBar = new ApplicationBar();
            ApplicationBar.BackgroundColor = color;
            ApplicationBar.IsMenuEnabled = true;
            ApplicationBar.IsVisible = true;
            ApplicationBar.Opacity = 0.6;

            ApplicationBarIconButton add = new ApplicationBarIconButton(new Uri("/Images/appbar_add.png", UriKind.Relative));
            add.Text = "add";
            add.Click += new EventHandler(add_Click);

            ApplicationBarIconButton refresh = new ApplicationBarIconButton(new Uri("/Images/appbar_refresh.png", UriKind.Relative));
            refresh.Text = "refresh";
            refresh.Click += new EventHandler(refresh_Click);

            ApplicationBarIconButton remove = new ApplicationBarIconButton(new Uri("/Images/appbar_delete.png", UriKind.Relative));
            remove.Text = "remove";
            remove.Click += new EventHandler(remove_Click);

            ApplicationBarIconButton info = new ApplicationBarIconButton(new Uri("/Images/appbar_info.png", UriKind.Relative));
            info.Text = "disclaimer";
            info.Click += new EventHandler(info_Click);

            ApplicationBar.Buttons.Add(add);
            ApplicationBar.Buttons.Add(refresh);
            ApplicationBar.Buttons.Add(remove);
            ApplicationBar.Buttons.Add(info);

            txtLastUpdated = new ApplicationBarMenuItem("...");
            ApplicationBar.MenuItems.Add(txtLastUpdated);

            control_MyStocks.goToMyStocksView += new EventHandler(control_MyStocks_goToMyStocksView);
            controlNews.goToNewsView += new EventHandler(controlNews_goToNewsView);

            refreshData();

            if (!IsolatedStorageSettings.ApplicationSettings.Contains("firsRun"))
            {
                showDisclaimer();
                IsolatedStorageSettings.ApplicationSettings.Add("firsRun", true);
            }
        }

        void control_MyStocks_goToMyStocksView(object sender, EventArgs e)
        {
            this.NavigationService.Navigate(new Uri("/Control_MyStocks_View.xaml", UriKind.Relative));
        }

        void controlNews_goToNewsView(object sender, EventArgs e)
        {
            this.NavigationService.Navigate(new Uri("/Control_News_View.xaml", UriKind.Relative));
        }

        void add_Click(object sender, EventArgs e)
        {
            this.NavigationService.Navigate(new Uri("/Control_MyStocks_Add.xaml", UriKind.Relative));
        }

        void remove_Click(object sender, EventArgs e)
        {
            this.NavigationService.Navigate(new Uri("/Control_MyStocks_Remove.xaml", UriKind.Relative));
        }

        void refresh_Click(object sender, EventArgs e)
        {
            refreshData();
        }

        void info_Click(object sender, EventArgs e)
        {
            showDisclaimer();
        }

        private void refreshData()
        {
            control_MyStocks.loadInfo();
            control_StockIndex.loadInfo();
            controlNews.loadInfo();

            txtLastUpdated.Text = "updated: " + DateTime.Now.ToString();
        }

        void showDisclaimer()
        {
            MessageBox.Show("DISCLAIMER" + Environment.NewLine + "Stock market data are provided by Yahoo! Finance. All quotes are delayed at least 15 minutes." + Environment.NewLine + Environment.NewLine + "All information are provided \"as is\" for informational purposes only, not intended for trading purposes or advice. Neither wallstreet nor Yahoo! is liable for any informational errors, incompleteness, or delays, or for any actions taken in reliance on information contained herein.");
        }
    }
}